﻿using Common.Log;
using DryWCS.App.Common;
using DryWCS.App.ViewModel.Base;
using DryWCS.Model.GlobalPara;
using DryWCS.Model.SysConfig;
using DryWCS.Model.User;
using DryWCS.Service.User;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace DryWCS.App.ViewModel.Settings
{
    public class UserManageModel:DataProcess<UserModel>
    {
        #region Parameter
        string msg = null;
        public new UserModel Model { get; set; } = new UserModel();
        UserService userService = new UserService();

        private List<UserModel> models = new List<UserModel>();
        #endregion

        #region Constructor
        public UserManageModel()
        {
            GridModelList = new ObservableCollection<UserModel>();
            QueryModels();
            ShowRowInfoCommand = new RelayCommand<object>(ShowRowInfo);
            AddModelCommand = new RelayCommand(AddModel);
            DelModelCommand = new RelayCommand<UserModel>(DeleteModel);
            EditModelCommand = new RelayCommand(EditModel);
        }


        #endregion

        #region Command
        public RelayCommand<object> ShowRowInfoCommand { get; set; }
        public RelayCommand AddModelCommand { get; set; }
        public RelayCommand<UserModel> DelModelCommand { get; set; }
        public RelayCommand EditModelCommand { get; set; }
        #endregion

        #region Double click event
        private void ShowRowInfo(object obj)
        {
            Model = obj as UserModel;
        }
        #endregion

        #region Qurey Config information
        private void QueryModels()
        {
            GridModelList.Clear();
            models = userService.GetList();
            if (models != null)
            {
                if (models.Any())
                {
                    models.ForEach(m =>
                    {
                        GridModelList.Add(m);
                    });
                }
            }
            GetPageData(PageIndex);
        }
        #endregion

        #region Add delete  edit
        private void AddModel()
        {
            if (Model == null)
            {
                Msg.Warning("请输入添加内容！");

            }
            msg = CheckHelper.CheckNullable(Model);
            if (msg != null)
            {
                Msg.Warning(msg);
                return;
            }
            else
            {
                if (userService.Insert(Model))
                {
                    msg = $"用户{GlobalPara.Loginer.Nikname} 插入数据{Model.Nikname}成功";
                }
                else
                {
                    msg = $"用户{GlobalPara.Loginer.Nikname} 插入数据{Model.Nikname}失败";
                }
                Msg.Info(msg);
                QueryModels();
            }
        }
        void DeleteModel(UserModel model)
        {
            if (model == null)
            {
                Msg.Info("请选择要删除的数据");
            }
            if (Msg.Question($"是否要删除{model.Id} 本条数据?"))
            {
                if (userService.Delete(model))
                {
                    msg = $"用户{GlobalPara.Loginer.Nikname} 删除{model.Id}数据成功";
                }
                else
                {
                    msg = $"用户{GlobalPara.Loginer.Nikname}删除{model.Id}数据失败";
                }
                Msg.Info(msg);
                LogHelper.WriteLog(msg);
            }
            // Messenger.Default.Register<bool>(this, "Del", DeleteInfo);
            QueryModels();


        }
        private void EditModel()
        {
            if (Model == null)
            {
                Msg.Warning("请选择编辑内容！");
            }
            msg = CheckHelper.CheckNullable(Model);
            if (msg != null)
            {
                Msg.Warning(msg);
                return;
            }
            else
            {
                if (userService.Update(Model))
                {
                    msg = $"用户{GlobalPara.Loginer.Nikname} 编辑数据{Model.Nikname}成功";
                }
                else
                {
                    msg = $"用户{GlobalPara.Loginer.Nikname} 编辑数据{Model.Nikname}失败";
                }
                Msg.Info(msg);
                QueryModels();
            }
        }
        #endregion

        #region 分页处理
        public override void GetPageData(int pageIndex)
        {
            try
            {
                Application.Current?.Dispatcher.Invoke(() =>
                {
                    PageSize = 5;
                    TotalCount = models.Count;
                    GridModelList.Clear();
                    models.Skip((pageIndex - 1) * PageSize).Take(PageSize).ToList().ForEach(p => GridModelList.Add(p));
                });

                base.SetPageCount();
            }
            catch (Exception ex)
            {
                Msg.Error(ex.Message);
            }
        }
        #endregion
    }
}
